var database = require('../../../data/data.js')
var app = getApp();
Page({
  data: {
    flag: true,
    flag1: true,
    flag2: true,
    flag3: true,
    choiceFlag: false,
    moneyFlag: false,
    team_tip: "",
    date_tip: "",
    time_tip: "",
    address_tip: "",
    people_tip: "",
    turf_tip: "",
    cost_tip: "",
    team: [],
    money: "",
    ensure: 0,
    award: 0,
    clothing: "",
    notes: "",
    week: "",
    people: ['11人', '10人', '9人', '8人', '7人', '6人', '5人', '4人', '3人'],
    turf: ['人造草', '天然草', '木地板'],
    cost: ['对手免费', 'AA制'],
    check_img: [
      { img: "../../../image/icon/nochoice.png" },
      { img: "../../../image/icon/nochoice.png" },
    ]
  },
  onLoad: function () {
    var that = this;
    // wx.request({
    //   url: database.url1 + '/api/V2/user/currency',
    //   data: {
    //     accessToken: database.accessToken,
    //     userId: database.userId
    //   },
    //   method: "POST",
    //   header: {
    //     'content-type': 'application/x-www-form-urlencoded'
    //   },
    //   success(res) {
    //     that.setData({
    //       balance: res.data.data.currency
    //     })
    //   }
    // });
    wx.request({
      url: database.url1 + '/api/V2/softball/softball_list',
      data: {
        accessToken: database.accessToken,
        userId: database.userId
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        var teamList = res.data.data.softballList;
        var team = [];
        for (var i = 0; i < teamList.length; i++) {
          team[i] = teamList[i].title
        }
        that.setData({
          team: team,
          teamList: teamList
        })
      }
    })
  },
  bindTeamChange: function (e) {
    this.setData({
      team_tip: "none",
      team_index: e.detail.value
    })
  },
  bindDateChange: function (e) {
    var date = new Date(e.detail.value);
    var week;
    if (date.getDay() == 0) week = "星期日"
    if (date.getDay() == 1) week = "星期一"
    if (date.getDay() == 2) week = "星期二"
    if (date.getDay() == 3) week = "星期三"
    if (date.getDay() == 4) week = "星期四"
    if (date.getDay() == 5) week = "星期五"
    if (date.getDay() == 6) week = "星期六"
    this.setData({
      date_tip: "none",
      date: e.detail.value,
      week: week
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time_tip: "none",
      time: e.detail.value
    })
  },
  bindPeopleChange: function (e) {
    this.setData({
      people_tip: "none",
      people_index: e.detail.value
    })
  },
  bindTurfChange: function (e) {
    this.setData({
      turf_tip: "none",
      turf_index: e.detail.value
    })
  },
  hide: function () {
    this.setData({
      flag: true,
      cost_tip: ""
    })
  },
  // hide1: function () {
  //   this.setData({
  //     flag3: true
  //   })
  // },
  change_img: function (e) {
    this.setData({
      choiceFlag: true
    })
    if (e.currentTarget.dataset.check == 1) {
      this.setData({
        flag1: false
      })
    } else {
      this.setData({
        flag1: true
      })
    }
    var check = e.currentTarget.dataset.check
    var check_img = [
      { img: "../../../image/icon/nochoice.png" },
      { img: "../../../image/icon/nochoice.png" },
    ]
    check_img[check].img = "../../../image/icon/choice.png"
    this.setData({
      check_img: check_img
    })
  },
  money_change: function (e) {
    if (e.detail.value != "") {
      this.setData({
        money: e.detail.value,
        moneyFlag: true
      })
    } else {
      this.setData({
        moneyFlag: false
      })
    }
  },
  choice_ok: function (e) {
    if (this.data.flag1 == true) {
      this.setData({
        money: "对手免费",
        flag: true,
        cost_tip: "none"
      })
    }
    if (this.data.flag2 == true) {
      if (this.data.money != "") {
        this.setData({
          flag: true,
          cost_tip: "none"
        })
      }
    }
  },
  bindCostChange: function (e) {
    this.setData({
      money: "",
      flag: false
    })
  },
  fight: function (e) {
    wx.redirectTo({
      url: '/pages/my/my_add_fight/my_add_fight',
    })
  },
  home: function (e) {
    wx.redirectTo({
      url: '/pages/home/home',
    })
  },
  choiceAddress: function () {
    var that = this;
    wx.chooseLocation({
      success(res) {
        that.setData({
          address_tip: "none",
          address: res.name,
          lat: res.latitude,
          lng: res.longitude
        })
      }
    })
  },
  ensureChange: function (e) {
    if (e.detail.value != "") {
      this.setData({
        ensure: e.detail.value
      })
    }
  },
  // awardChange: function (e) {
  //   if (e.detail.value != "") {
  //     this.setData({
  //       award: e.detail.value
  //     })
  //   }
  // },
  clothingChange: function (e) {
    if (e.detail.value != "") {
      this.setData({
        clothing: e.detail.value
      })
    }
  },
  notesChange: function (e) {
    if (e.detail.value != "") {
      this.setData({
        notes: e.detail.value
      })
    }
  },
  submit: function (e) {
      var that = this;
      var data = e.detail.value;
      var teamid = this.data.teamList[this.data.team_index].id
      var people = data.people.substring(0, data.people.length - 1);
      var money = that.data.money;
      if (money == "对手免费") {
        money = 0;
      } else {
        money = that.data.money;
      }
      // console.log("teamid:" + teamid + "    popenid:" + database.userId + "     date:" + data.date + "    week:" + that.data.week + "    time:" + data.time + "    lat:" + that.data.lat + "    lng" + that.data.lng + "    adress:" + that.data.address + "    contact" + database.tel + "    person:" + people + "    clothing:" + data.clothing + "     turf:" + data.turf + "    cost:" + money + "    ensure:" + that.data.ensure + "    award:" + that.data.award + "    notes" + data.notes);
      wx.request({
        url: database.url + '/project/uploadContest.action',
        header: {
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        },
        data: {
          pteamid: teamid,
          popenid: database.userId,
          date: data.date,
          week: that.data.week,
          time: data.time,
          lat: that.data.lat,
          lng: that.data.lng,
          adress: that.data.address,
          contact: database.tel,
          person: people,
          clothing: data.clothing,
          turf: data.turf,
          cost: money,
          ensure: that.data.ensure,
          award: that.data.award,
          notes: data.notes
        },
        success(res) {
          that.setData({
            flag2: false
          })
        }
      })
     
  },
  // toRecharge: function () {
  //   wx.navigateTo({
  //     url: '/pages/my/recharge/recharge',
  //     flag3: true
  //   })
  // }
})